const Appconfig = require('./config.js');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const path = require('path');
module.exports = function() {
  return {
    module: {
      rules: [
        // {
        //   enforce: 'pre', //强制预处理
        //   test: /\.js$/,
        //   exclude: /node_modules/, //不处理对应的node_modules
        //   loader: "eslint-loader"
        // },
        {
          test: /\.js$/,
          exclude: /node_modules/,
          //只对app目录的代码进行babel编译
          include: path.resolve(__dirname, 'app'),
          use: [
            {
              loader: 'babel-loader',
              options: {
                presets: ['es2015'],
                //开启babel缓存 默认为false 如果开启减少编译时间
                cacheDirectory: false
              }
            }
          ]
        },
        {
          test: /\.(gif|png|jepg)$/,
          use: [
            {
              loader: 'url-loader',
              options:{
                limit:8192,
                name:"img/[name].[hash:8].[ext]"
              }
            }
          ]
        }
      ]
    },
    resolve: {
      extensions: ['.js'], //**Change
      //写绝对路径能够减少搜索
      modules: ['node_modules'],
      // tree-sharking 是指借助es6 import export 语法静态性的特点来删除export 但是没有import过的东西。
    },
    plugins: [new webpack.DefinePlugin(
      {
        SERVERHOST: Appconfig.host,
        DEBUG: Appconfig.debug
      }),
      new ExtractTextPlugin('css/[name]-[contenthash].css'),
      new HtmlWebPackPlugin({title: '你好', template: 'src/index.html'}),
    ]
  }
}
